package com.example.mp3player01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.model.Mp3Info;
import com.example.utils.FileUtils;

/**
 * @author congwang
 *
 */
public class LocalMp3ListActivity extends ListActivity {
	private List<Mp3Info> mp3Infos;
	FileUtils fileUtils;
	private String mp3Folder = "mp3/";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mp3Infos = new ArrayList<Mp3Info>();
		fileUtils = new FileUtils();
		fileUtils.creatSDDir(mp3Folder);
		setContentView(R.layout.local_mp3_list);
	}
	
	protected void onResume() {
		super.onResume();
		mp3Infos = fileUtils.getMp3Files(mp3Folder);
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		
		if (mp3Infos != null) {
			for (Mp3Info mp3: mp3Infos) {
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("mp3_name", mp3.getMp3Name());
				map.put("mp3_size", mp3.getMp3Size());
				list.add(map);
			}
		}
		
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.mp3info_item, new String[] {"mp3_name", "mp3_size"}, 
				new int[] {R.id.mp3_name, R.id.mp3_size});
		setListAdapter(simpleAdapter);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		if (mp3Infos != null) {
			Mp3Info mp3 = mp3Infos.get(position);
			Intent intent = new Intent();
			intent.putExtra("mp3Info", mp3);
			intent.setClass(this, PlayerActivity.class);
			startActivity(intent);
		}
		
	}
	
	
	
}
